package com.dao.impl;

import com.dao.UserDao;
import com.models.User;
import com.persistence.UserPersistence;

/**
 * Created by TimeLiar on 2015/9/22.
 *
 */
public class UserDaoImpl implements UserDao {
    @Override
    public void addUser(User user) {
        synchronized (UserPersistence.class){
            UserPersistence.getUsers().add(user);
            UserPersistence.persistence();
        }
    }

    @Override
    public void deleteUser(User user) {
        synchronized (UserPersistence.class){
            UserPersistence.getUsers().remove(user);
            UserPersistence.persistence();
        }
    }

    @Override
    public void updateUser(User user) {
        synchronized (UserPersistence.class) {
            UserPersistence.getUsers().remove(user);
            UserPersistence.getUsers().add(user);
            UserPersistence.persistence();
        }
    }
    @Override
    public User getUser(String name) {
        for(User user:UserPersistence.getUsers()){
            if(user.getUsername() != null && user.getUsername().equals(name)){
                return user;
            }
        }
        return null;
    }
}
